#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"

main(int argc, char **argv)
{
	int fd;
	char r_buf[100];
	int nread;
	if(mkfifo(FIFO, O_RDONLY) < 0)
		printf("cannot create fifo server\n");
	printf("Preparing for reading bytes...\n");
	memset(r_buf,0,sizeof(r_buf));
	/*open the named pipe,and set the nonblock flag*/
	//fd = open(FIFO,O_RDONLY|O_NONBLOCK,0);
	fd = open(FIFO,O_RDONLY,0);//读取数据的时候，第一次读取会堵塞进程，后面的就不会阻塞了，为什么????????
	if(fd == -1)
	{
		perror("open error");
		exit(1);
	}
	while(1)
	{
		memset(r_buf,0,sizeof(r_buf));
		if((nread = read(fd,r_buf,100)) == -1)
		{
			if(errno == EAGAIN)
				printf("no data yet\n");
		}
		printf("read %s from FIFO\n", r_buf);
		sleep(1);
	}
	pause();
	unlink(FIFO);
}
